//
//  QQListController.m
//  Objc进阶Demo
//
//  Created by Don't update on 2018/8/15.
//  Copyright © 2018年 Don't update. All rights reserved.
//

#import "QQListController.h"
#import "groupModel.h"
@interface QQListController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *dataSource;
@end

@implementation QQListController




-(UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}


-(NSMutableArray *)dataSource{
    if (_dataSource == nil) {
        _dataSource = [[NSMutableArray alloc]init];
        
        NSDictionary *JSONDic =@{@"group":
                                     @[
                                         @{@"groupName":@"小学同学",@"groupCount":@"3",@"groupArray":@[
                                                   @{@"name":@"小明",@"avatarURL":@"",@"shuoshuo":@"作业又没写好,唉！",@"status":@"1"},
                                                   @{@"name":@"小红",@"avatarURL":@"",@"shuoshuo":@"考试不要抄我的！",@"status":@"1"},
                                                   @{@"name":@"小王",@"avatarURL":@"",@"shuoshuo":@"马勒戈壁有本事放学别走！",@"status":@"0"}
                                                   ]},
                                         @{@"groupName":@"初中同学",@"groupCount":@"5",@"groupArray":
                                               @[
                                                   @{@"name":@"王二小",@"avatarURL":@"",@"shuoshuo":@"我家来自农村，不要欺负我",@"status":@"1"},
                                                   @{@"name":@"王麻子",@"avatarURL":@"",@"shuoshuo":@"历史咯老师真漂亮！",@"status":@"1"},
                                                   @{@"name":@"吴道德",@"avatarURL":@"",@"shuoshuo":@"我姓吴，法号道德",@"status":@"1"},
                                                   @{@"name":@"张丝丹",@"avatarURL":@"",@"shuoshuo":@"我小名叫四蛋子，哈哈",@"status":@"0"},
                                                   @{@"name":@"赵铁柱",@"avatarURL":@"",@"shuoshuo":@"我喜欢小花",@"status":@"0"}
                                                   ]},
                                         @{@"groupName":@"高中同学",@"groupCount":@"3",@"groupArray":
                                               @[
                                                   @{@"name":@"刘阿猫",@"avatarURL":@"",@"shuoshuo":@"我操，高考又到了",@"status":@"1"},
                                                   @{@"name":@"静静",@"avatarURL":@"",@"shuoshuo":@"大家好，我是静静。",@"status":@"1"},
                                                   @{@"name":@"隔壁老王",@"avatarURL":@"",@"shuoshuo":@"小样你是新来的吧！",@"status":@"0"}
                                                   ]},
                                         @{@"groupName":@"大学同学",@"groupCount":@"4",@"groupArray":
                                               @[
                                                   @{@"name":@"屌丝男",@"avatarURL":@"",@"shuoshuo":@"泡妞去了，回聊。",@"status":@"1"},
                                                   @{@"name":@"游戏狗",@"avatarURL":@"",@"shuoshuo":@"我擦，双杀！！",@"status":@"1"},
                                                   @{@"name":@"学霸",@"avatarURL":@"",@"shuoshuo":@"期末考试稳拿第一",@"status":@"1"},
                                                   @{@"name":@"书呆子",@"avatarURL":@"",@"shuoshuo":@"蛋白质是怎么炼成的。。。",@"status":@"0"}]},
                                         @{@"groupName":@"同事",@"groupCount":@"3",@"groupArray":
                                               @[
                                                   @{@"name":@"JAVA工程师",@"avatarURL":@"",@"shuoshuo":@"JAVA是最好的编程语言",@"status":@"1"},
                                                   @{@"name":@"Android工程师",@"avatarURL":@"",@"shuoshuo":@"Android最好用，便宜耐摔！",@"status":@"1"},
                                                   @{@"name":@"iOS工程师",@"avatarURL":@"",@"shuoshuo":@"iPhone手机牛逼又流畅。",@"status":@"0"}
                                                   ]},
                                         @{@"groupName":@"家人",@"groupCount":@"3",@"groupArray":
                                               @[
                                                   @{@"name":@"妈妈",@"avatarURL":@"",@"shuoshuo":@"今天天气好晴朗☀️，处处好风光",@"status":@"1"},
                                                   @{@"name":@"爸爸",@"avatarURL":@"",@"shuoshuo":@"农家乐！",@"status":@"1"},
                                                   @{@"name":@"姐姐",@"avatarURL":@"",@"shuoshuo":@"唱歌跳舞样样精通。",@"status":@"0"}
                                                   ]}
                                         ]
                                 };
        for (NSDictionary *groupInfoDic in JSONDic[@"group"]) {
            groupModel *model = [[groupModel alloc]init];
            model.groupName = groupInfoDic[@"groupName"];
            model.groupCount = [groupInfoDic[@"groupCount"] integerValue];
            model.isOpen = NO;
            model.friendList = groupInfoDic[@"groupArray"];
            [_dataSource addObject:model];
        }
        
    }
    return _dataSource;
}

#pragma mark -------dataSourseDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    groupModel *model = _dataSource[section];
    NSInteger count =model.isOpen?model.friendList.count:0;
    return  count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cell_Identify = @"cell_Identify";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_Identify];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cell_Identify];
    }
    groupModel *groupModel = _dataSource[indexPath.section];
    NSDictionary *friendInfo = groupModel.friendList[indexPath.row];
    cell.textLabel.text = friendInfo[@"name"];
    cell.detailTextLabel.text = friendInfo[@"shuoshuo"];
    return cell;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _dataSource.count;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *selectionView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
    selectionView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:0.8];
    groupModel *groupModel = _dataSource[section];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setFrame:selectionView.bounds];
    [btn setTag:section];
    [btn setTitle:groupModel.groupName forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
    [selectionView addSubview:btn];
    return selectionView;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}

-(void)buttonPress:(UIButton *)sender{
    groupModel *groupModel = _dataSource[sender.tag];
    groupModel.isOpen = !groupModel.isOpen;
    [_tableView reloadSections:[NSIndexSet indexSetWithIndex:sender.tag] withRowAnimation:UITableViewRowAnimationAutomatic];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self dataSource];
    [self tableView];
  
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
